package com.mhc.suzuki.controller;

import com.mhc.haval.security.model.User;
import com.mhc.suzuki.bo.FinSettlementBO;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.form.FinSettlementForm;
import com.mhc.suzuki.service.FinSettlementService;
import com.mhc.suzuki.util.BeanCopierUtil;
import com.mhc.suzuki.util.DateUtil;
import com.mhc.suzuki.vo.FinSettlementVO;
import com.subaru.common.entity.BizResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author Churry
 * @create 2017-08-24 12:38
 **/
@Slf4j
@RestController
@RequestMapping("/order/finSettlement")
public class FinanceController {

    @Autowired
    FinSettlementService finSettlementService;

    /**
     * 根据订单编号，获取订单详情中的的财务结算详情
     *
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "获取财务结算详情", notes = "获取订单详情中的的财务结算详情")
    @ApiImplicitParam(name = "orderNo", value = "订单编号", required = true, paramType = "path", dataType = "String")
    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.GET)
    public Object getFinSettlementDetail(@PathVariable String orderNo) {
        //获取并返回数据
        FinSettlementVO finSettlementVO = BeanCopierUtil.convert(finSettlementService.getFinSettlementDetail(orderNo), FinSettlementVO.class);
        return BizResult.create(finSettlementVO, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
    }

    /**
     * 修改订单详情中的的财务结算详情
     *
     * @param finSettlementForm
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "修改财务结算详情", notes = "修改订单详情中的的财务结算详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "finSettlementForm", value = "财务结算详情表单", required = true, dataType = "FinSettlementForm"),
            @ApiImplicitParam(name = "orderNo", value = "订单编号", required = true, paramType = "path", dataType = "String")})
    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.POST)
    public Object saveFinSettlementDetail(@AuthenticationPrincipal User logedUser, @RequestBody FinSettlementForm finSettlementForm, @PathVariable String orderNo) {
        Optional<User> userOptional = Optional.ofNullable(logedUser);
        if (userOptional.isPresent()) {
            //保存数据
            try {
                FinSettlementBO finSettlementBO = BeanCopierUtil.convert(finSettlementForm, FinSettlementBO.class);
                if (!finSettlementForm.getBankLoanDate().isEmpty()) {
                    finSettlementBO.setBankLoanDate(DateUtil.defaultParseStringToDateTime(finSettlementForm.getBankLoanDate()));
                }
                return BizResult.create(null, finSettlementService.saveFinSettlementDetail(logedUser.getStaffId(), finSettlementBO, orderNo), ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
            } catch (Exception e) {
                log.error("ERROR IN saveFineSettlementDetail", e);
                return BizResult.create(ErrorCodeEnum.SYS_ERROR.getErrCode(), e.getMessage());
            }
        } else {
            return BizResult.create(ErrorCodeEnum.NOT_LOGIN.getErrCode(), ErrorCodeEnum.NOT_LOGIN.getErrMsg());
        }
    }
}
